using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Linq;
using DG;
using DG.Tweening;
using Sirenix.OdinInspector;
using UnityEngine.UIElements;

public class UniTask_DotweenExample : MonoBehaviour
{
    public GameObject moveObj;
    public Image image;
    
    // parallel with cancellation
    private CancellationToken parallelTestToken;
    void Start()
    {
  
    }
 
    [Button("重置物体初始状态")]
    private void Reset()
    {
        moveObj.transform.localPosition = Vector3.zero;
    }

    [Button("串行测试")]
    public void Test1()
    {
        StartMoveObectAsync().Forget();
    }

    private async UniTaskVoid StartMoveObectAsync()
    {
        await moveObj.transform.DOMoveX(10, 2);
        Debug.Log("到达终点");
        await moveObj.transform.DOMoveX(0, 1f);
        Debug.Log("返回起点");
    }

    [Button("并行测试")]
    public void Test2()
    {
        ParallelMoveObjectAsync().Forget();
    }
    
    private async UniTaskVoid ParallelMoveObjectAsync()
    {
        parallelTestToken = this.GetCancellationTokenOnDestroy();
        
        await UniTask.WhenAll(
            moveObj.transform.DOMoveX(10, 3f).WithCancellation(parallelTestToken),
            moveObj.transform.DOMoveY(10, 3f).WithCancellation(parallelTestToken)
        );
        await UniTask.WhenAll(
            moveObj.transform.DOMoveX(0, 3f).WithCancellation(parallelTestToken),
            moveObj.transform.DOMoveY(0, 3f).WithCancellation(parallelTestToken)
        );
    }
    
}
